//
//  NKSettingViewController.m
//  NewKY
//
//  Created by Dee on 2016/11/14.
//  Copyright © 2016年 Dee. All rights reserved.
//

#import "NKSettingViewController.h"
#import "NKSettingTableViewCell.h"
#import "NKSettingModel.h"
#import "NKFeedbackVC.h"
#import <SDImageCache.h>
#import "ClearCacheTool.h"

#define NKSettingCellID @"NKSettingCellID"

@interface NKSettingViewController ()<UITableViewDataSource,UITableViewDelegate>

/*---- 模型数据 -----*/
@property(nonatomic,strong)NSArray *modelArr;

/*---- table -----*/
@property(nonatomic,weak)UITableView *tableView;

@end

@implementation NKSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    [self setupUI];
    
}

-(void)setupUI{
    
    //================== 添加tableView =================//
    UITableView *tab = [[UITableView alloc]initWithFrame:self.view.bounds];
    tab.dataSource = self;
    tab.delegate = self;
//    tab.rowHeight = 60;
    UIImageView *bgView =  [[UIImageView alloc] initWithFrame:self.view.bounds];
    bgView.image = [UIImage imageNamed:@"An_Background"];
    bgView.contentMode = UIViewContentModeScaleAspectFill;
    tab.backgroundView = bgView;
    
    [tab registerNib:[UINib nibWithNibName:@"NKSettingTableViewCell" bundle:nil] forCellReuseIdentifier:NKSettingCellID];
    
    [self.view addSubview:tab];

    self.tableView = tab;
    
    tab.separatorStyle = UITableViewCellSeparatorStyleNone;
    
}

#pragma mark 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.modelArr.count;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NKSettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NKSettingCellID forIndexPath:indexPath];
    NKSettingModel *model = self.modelArr[indexPath.row];
    cell.settingModel = model;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    return cell;
    
}

#pragma mark - 代理
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    /*---- 取消选中状态 -----*/
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    switch (indexPath.row) {
        case 0:
        case 1:{
         /*---- 跳转到另一界面 -----*/
            NKFeedbackVC *Feedback = [[NKFeedbackVC alloc]init];
            NKSettingModel *model = self.modelArr[indexPath.row];
            Feedback.title = model.title;
            
            [self.navigationController pushViewController:Feedback animated:YES];
        }
            break;
        case 2:
        {
             /*---- 修改密码 -----*/
            
            UIViewController *vc = [NSClassFromString(@"NKChangePasswordUserViewController") new];
            
            [[self getMainNavigation] pushViewController:vc animated:YES];
            
        }
            break;
        case 3:{
            float tmpSize = [[SDImageCache sharedImageCache] getSize]/1024;
            NSString *tmp = [NSString stringWithFormat:@"当前缓存大小为%.1fKB",tmpSize];
    
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定要清理缓存吗?" message:tmp preferredStyle:UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
              
               /*---- 清理缓存 -----*/
                [[SDImageCache sharedImageCache] clearDisk];
                NSString *fullPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
                [ClearCacheTool clearCacheWithFilePath:fullPath];
                [SVProgressHUD showSuccessWithStatus:@"清理成毕"];
                
            }]];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:NULL]];
            
            [self presentViewController:alert animated:YES completion:nil];
            
        }
            break;
        case 4:
        {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提醒" message:@"您确定要注销吗?" preferredStyle:UIAlertControllerStyleAlert];
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                 /*---- 点击了确定,注销 -----*/
                
                NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
                [userDefaults setObject:@"0" forKey:@"USER_STATUS"];
                [userDefaults removeObjectForKey:@"NOW_USER_PHONE"];
                [[self getLeftViewController].tableView reloadData];
                [[self getMainNavigation] showHomeViewController];
                
                [SVProgressHUD showSuccessWithStatus:@"注销成功"];
                
            }]];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
            
            [self presentViewController:alert animated:YES completion:nil];
        }

        default:
            break;
    }
}



#pragma mark  解析Plist
-(NSArray *)modelArr{
    
    if(_modelArr == nil){
        
        NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"medtronic.plist" ofType:nil]];
        NSMutableArray *arrM = [NSMutableArray array];
        
        for (NSDictionary *dict in arr) {
            
            [arrM addObject:[NKSettingModel modelWithDict:dict]];
        }
        _modelArr = arrM;
    }
    
    return _modelArr;
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




@end
